package com.xjl.weatherforecast.job;
import com.xjl.weatherforecast.service.WeatherDataCollectionService;
import com.xjl.weatherforecast.vo.City;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.ArrayList;
import java.util.List;


public class WeatherDataSyncJob extends QuartzJobBean {

    private final static Logger logger = LoggerFactory.getLogger(WeatherDataSyncJob.class);
   /* @Autowired
    private CityDataService cityDataService;
    @Autowired
    private WeatherDataService weatherDataService;*/
   @Autowired
   private WeatherDataCollectionService weatherDataCollectionService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("Weather data sync job");
        // 获取城市ID列表
        List<City> cityList = null;
        try {
          // cityList = cityDataService.listCity();
            // TODO 改为由城市数据API微服务提供数据
            cityList = new ArrayList<>();
            City city = new City();
            city.setCityId("101280601");
            cityList.add(city);
        } catch (Exception e) {
            logger.error("Exception!", e);
        }
        // 遍历城市ID获取天气
        for (City city : cityList) {
            String cityId = city.getCityId();
            logger.info("Weather Data Sync Job, cityId:" + cityId);
            weatherDataCollectionService.syncDateByCityId(cityId);
        }
        logger.info("Weather Data Sync Job. End！");
    }
}
